package org.example.erpapiservicespringpgimpl;

import org.example.erpapicommon.Util.BasePage;
import org.example.erpapicommon.Util.Pageable;
import org.example.erpapicommon.Util.Param.*;
import org.example.erpapicommon.model.*;
import org.example.erpapicommon.service.OtherService;
import org.example.erpapiservicespringpgimpl.Dao.CategoryDao;
import org.example.erpapiservicespringpgimpl.Dao.OtherDao;
import org.example.erpapiservicespringpgimpl.Dao.SupplierDao;
import org.example.erpapiservicespringpgimpl.entity.*;
import org.example.erpapiservicespringpgimpl.mapper.*;

import org.seasar.doma.jdbc.SelectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class OtherServicePgImpl implements OtherService {
    @Autowired
    StockMapper stockMapper;
    @Autowired
    ShipperMapper shipperMapper;
    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    SupplierDao supplierDao;
    @Autowired
    CategoryDao categoryDao;
    @Autowired
    OtherDao otherDao;
    @Override
    public List<SimpleSupplier> findSimpleSuppliersByProductsId(Integer id) {
        List<SupplierEntity> entities = supplierDao.findByProductId(id);
        return supplierMapper.fromSimpleListEntity(entities);
    }

    @Override
    public List<SimpleCategory> findSimpleCategoriesById(String id) {
        List<CategoryEntity> entities = categoryDao.findById(id);
        return categoryMapper.fromCategoriesToSimpleCategories(entities);

    }

    @Override
    @Cacheable(value = "erp_api",key = "'pageEmployees_'+#param.toString()", condition = "#param != null")
    public Pageable<TEmployee> findEmployees(EmployeeQueryParam param) {
        SelectOptions options = SelectOptions.get().count();
        List<EmployeeEntity> employeeList = otherDao.findEmployees(param, options);
        int totalCount = (int) options.getCount();
        int pageNumber = param.getPageNumber();
        int pageSize = param.getPageSize();
        int totalPage = PageUtil.getTotalPage(totalCount, pageSize);
        int offset = PageUtil.getOffset(pageNumber, pageSize);
        options.offset(offset).limit(pageSize);
        List<EmployeeEntity> employeeEntityList = otherDao.findEmployees(param, options);
        List<TEmployee> tEmployeeList = employeeMapper.fromEntities(employeeEntityList);
        Pageable<TEmployee> tEmployeePageable = new BasePage<>(tEmployeeList, pageNumber, pageSize, totalPage, totalCount);
        return tEmployeePageable;
    }

    @Override
    @Cacheable(value = "erp_api",key = "'pageCustomers_'+#param.toString()", condition = "#param != null")
    public Pageable<TCustomer> findCustomers(CustomerQueryParam param) {
        SelectOptions options = SelectOptions.get().count();
        List<CustomerEntity> customerList = otherDao.findCustomers(param, options); // 注意：这里假设 findEmployees 方法可以处理 CustomerEntity
        int totalCount = (int) options.getCount();
        int pageNumber = param.getPageNumber();
        int pageSize = param.getPageSize();
        int totalPage = PageUtil.getTotalPage(totalCount, pageSize);
        int offset = PageUtil.getOffset(pageNumber, pageSize);
        options.offset(offset).limit(pageSize);
        List<CustomerEntity> customerEntityList = otherDao.findCustomers(param, options);
        List<TCustomer> tCustomerList = customerMapper.fromEntities(customerEntityList);
        Pageable<TCustomer> tCustomerPageable = new BasePage<>(tCustomerList, pageNumber, pageSize,totalPage,  totalCount);
        return tCustomerPageable;
    }

    @Override
    @Cacheable(value = "erp_api",key = "'pageShippers_'+#param.toString()", condition = "#param != null")
    public Pageable<TShipper> findShippers(ShipperQueryParam param) {
        SelectOptions options = SelectOptions.get().count();
        List<ShipperEntity> shipperList = otherDao.findShippers(param, options);
        int totalCount = (int) options.getCount();
        int pageNumber = param.getPageNumber();
        int pageSize = param.getPageSize();
        int totalPage = PageUtil.getTotalPage(totalCount, pageSize);
        int offset = PageUtil.getOffset(pageNumber, pageSize);
        options.offset(offset).limit(pageSize);
        List<ShipperEntity> shipperEntityList = otherDao.findShippers(param, options);
        List<TShipper> tShipperList = shipperMapper.fromEntityList(shipperEntityList);
        Pageable<TShipper> tShipperPageable = new BasePage<>(tShipperList, pageNumber,  pageSize,totalPage, totalCount);
        return tShipperPageable;
    }

    @Override
    @Cacheable(value = "erp_api",key = "'pageSuppliers_'+#param.toString()", condition = "#param != null")
    public Pageable<Supplier> findSuppliers(SupplierQueryParam param) {
        SelectOptions options = SelectOptions.get().count();
        List<SupplierEntity> supplierList = supplierDao.pageSupplier(param, options);
        int totalCount = (int) options.getCount();
        int pageNumber = param.getPageNumber();
        int pageSize = param.getPageSize();
        int totalPage = PageUtil.getTotalPage(totalCount, pageSize);
        int offset = PageUtil.getOffset(pageNumber, pageSize);
        options.offset(offset).limit(pageSize);
        List<SupplierEntity> supplierEntityList = supplierDao.pageSupplier(param, options);
        List<Supplier> tSupplierList = supplierMapper.fromListEntity(supplierEntityList);
        Pageable<Supplier> tSupplierPageable = new BasePage<>(tSupplierList, pageNumber, pageSize, totalPage, totalCount);
        return tSupplierPageable;
    }

    @Override
    @Cacheable(value = "erp_api",key = "'findCategoriesByLevel_'+#level")
    public List<TCategory> findCategoriesByLevel(Integer level) {
        List<CategoryEntity> categoryEntities=categoryDao.findCategoriesByLevel(level);
        return categoryMapper.fromCategoriesToTCategories(categoryEntities);
    }

    @Override
    @Cacheable(value = "erp_api",key = "'pageStocks_'+#param.toString()", condition = "#param != null")
    public Pageable<Stock> findPurchases(PurchaseQueryParam param) {
        SelectOptions options = SelectOptions.get().count();
        List<StockEntity> stockList = otherDao.findPurchases(param, options);
        int totalCount = (int) options.getCount();
        int pageNumber = param.getPageNumber();
        int pageSize = param.getPageSize();
        int totalPage = PageUtil.getTotalPage(totalCount, pageSize);
        int offset = PageUtil.getOffset(pageNumber, pageSize);
        options.offset(offset).limit(pageSize);
        List<StockEntity> stockEntityList = otherDao.findPurchases(param, options);
        List<Stock> tStockList = stockMapper.toStockList(stockEntityList);
        Pageable<Stock> tStockPageable = new BasePage<>(tStockList, pageNumber, pageSize, totalPage, totalCount);
        return tStockPageable;
    }
}
